/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * edit_inventory.java
 *
 * Created on Jul 25, 2011, 1:55:00 AM
 */
package introse1;

import java.util.ArrayList;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
 *
 * @author Trisha
 */
public class edit_inventory extends javax.swing.JFrame {

    Connection conn;
    Statement stmt, stmt2;
    ResultSet result1, result2;
    int flag = 1;

    String user = "root";
    String pass = "p@ssword";
    String URL= "jdbc:mysql://localhost:3306/hajkebab";

    /** Creates new form edit_inventory */
    public edit_inventory(manage_inventory manage_inventory, String selectedPangalan) {
        selectedName = selectedPangalan;
        initComponents();
        
        editInventory();
        editInventoryIDTextField.setEnabled(false);
        
//        System.out.println("This is inside edit_inventory: " + selectedName);
        this.manage_inventory = manage_inventory;
        manage_inventory.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        editInventoryNameTextField = new javax.swing.JTextField();
        UnitofMeasureComboBox = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        editInventoryQuantityTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        editInventoryPriceTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        editInventoryIDTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        editInventoryUsedTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        BackButton = new javax.swing.JButton();
        SaveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel2.setText("Name:");

        jLabel3.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel3.setText("Unit of Measure:");

        UnitofMeasureComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kilograms", "Grams" }));

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Edit Inventory");

        jLabel5.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel5.setText("Quantity:");

        jLabel4.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel4.setText("Price:");

        jLabel6.setFont(new java.awt.Font("Rockwell", 0, 18)); // NOI18N
        jLabel6.setText("ID:");

        jLabel7.setFont(new java.awt.Font("Rockwell", 0, 18)); // NOI18N
        jLabel7.setText("Used:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(editInventoryQuantityTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(UnitofMeasureComboBox, 0, 211, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(editInventoryIDTextField)
                                    .addComponent(editInventoryNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(87, 87, 87)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(editInventoryPriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(editInventoryUsedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editInventoryIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editInventoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editInventoryQuantityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(UnitofMeasureComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editInventoryPriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editInventoryUsedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(6, 6, 6))
        );

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        SaveButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        SaveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/save.png"))); // NOI18N
        SaveButton.setBorder(null);
        SaveButton.setBorderPainted(false);
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addComponent(SaveButton)
                .addGap(38, 38, 38)
                .addComponent(BackButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveButton)
                    .addComponent(BackButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        DBConnect con = new DBConnect();
        Connection password = con.connect();
        PreparedStatement pstmt;
        ResultSet res;
        
//        var.setInventory_id(Integer.parseInt(editInventoryIDTextField.getText()));
//        var.setInventory_name(editInventoryNameTextField.getText());
//        var.setUnit_of_measure(UnitofMeasureComboBox.getSelectedItem().toString());
//        var.setInventory_quantity(Float.parseFloat(editInventoryQuantityTextField.getText()));
//        var.setInventory_price(Float.parseFloat(editInventoryPriceTextField.getText()));
//        var.setUsed_ingredient(Float.parseFloat(editInventoryUsedTextField.getText()));
//        man.editInventory(var);

        int id = Integer.parseInt(editInventoryIDTextField.getText());
        String name = editInventoryNameTextField.getText();
        String measure = UnitofMeasureComboBox.getSelectedItem().toString();
        float quantity = Float.parseFloat(editInventoryQuantityTextField.getText());
        float price = Float.parseFloat(editInventoryPriceTextField.getText());
        float used = Float.parseFloat(editInventoryUsedTextField.getText());
        
        String quantity1 = String.valueOf(quantity);
        String price1 = String.valueOf(price);
        String used1 = String.valueOf(used);

        if(name.isEmpty() || quantity1.isEmpty() || price1.isEmpty() || measure.isEmpty() || used1.isEmpty()){
            JOptionPane.showMessageDialog(this, "Must complete all required information", "Edit Failed", JOptionPane.ERROR_MESSAGE);
        }else{
             try{
                pstmt = password.prepareStatement("UPDATE inventory SET inventoryName = ?, quantity = ?, UnitofMeasure = ?, price = ?, ingredientUsed = ? WHERE inventoryID = ? ");
                pstmt.setString(1, name);
                pstmt.setFloat(2, quantity);
                pstmt.setString(3, measure);
                pstmt.setFloat(4, price);
                pstmt.setFloat(5, used);
                pstmt.setInt(6, id);

                pstmt.executeUpdate();
                this.setVisible(false);
                manage_inventory.setVisible(true);
               }catch(Exception e){ }
        }


    }//GEN-LAST:event_SaveButtonActionPerformed

    public void setVars(VariableList list){
        var = list;
    }

    public VariableList getVars(){
        return var;
    }

    public void editInventory(){
    String measure = UnitofMeasureComboBox.getSelectedItem().toString();

        try{
        DBConnect conn = new DBConnect();
        Connection con = conn.connect();
        Statement stmt = (Statement) con.createStatement();

            String SQL = "Select * from inventory where inventoryName='" + selectedName + "'";
            ResultSet rs = stmt.executeQuery(SQL);
            if(rs.next()){
                    editInventoryIDTextField.setText(rs.getString("inventoryID"));
                    editInventoryNameTextField.setText(rs.getString("inventoryName"));
                    editInventoryPriceTextField.setText(rs.getString("price"));
                    editInventoryQuantityTextField.setText(rs.getString("quantity"));
                    editInventoryUsedTextField.setText(rs.getString("ingredientUsed"));

            }else
                {
                    JOptionPane.showMessageDialog(this, "Item does not exist", "Search failed", JOptionPane.ERROR_MESSAGE);
                }

    }catch(Exception e){
        System.out.println("Error: " + e.getMessage());
    }
    }

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        manage_inventory.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new edit_inventory(manage_inventory, selectedName).setVisible(true);
            }
        });
    }
    public static VariableList var = new VariableList();
    public static InventoryManager man = new InventoryManager();
    private static String selectedName;
    private static manage_inventory manage_inventory;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BackButton;
    private javax.swing.JButton SaveButton;
    private javax.swing.JComboBox UnitofMeasureComboBox;
    private javax.swing.JTextField editInventoryIDTextField;
    private javax.swing.JTextField editInventoryNameTextField;
    private javax.swing.JTextField editInventoryPriceTextField;
    private javax.swing.JTextField editInventoryQuantityTextField;
    private javax.swing.JTextField editInventoryUsedTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
